﻿using NovoSerDes.Funcs;
using NovoUSB2XXX;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using static NovoSerDes.Config;

namespace NovoSerDes.Comm
{
    public class NovoCommWIthLog : INovoComm
    {
        private ILogData _logData;

        private static bool _logSwtich = false;

        public static bool LogSwtich
        {
            set { _logSwtich = value; }
        }
        public NovoCommWIthLog()
        {
            _logData = LogData.Instance;
        }

        public int Novo_CloseDevice()
        {
            return NovoComm.Novo_CloseDevice();
        }

        public int Novo_CreateSdk()
        {
            return NovoComm.Novo_CreateSdk();
        }

        public void Novo_DeleteSdk()
        {
            NovoComm.Novo_DeleteSdk();
        }

        public int Novo_EnumDevice()
        {
            return NovoComm.Novo_EnumDevice();
        }

        public int Novo_GetDevInfo(ref NOVO_DEV_INFO pDevInfo)
        {
            return NovoComm.Novo_GetDevInfo(ref pDevInfo);
        }

        public void Novo_GetDevName(StringBuilder devName)
        {
            NovoComm.Novo_GetDevName(devName);
        }

        public int Novo_GPIO_Get(byte mask, ref byte pData)
        {
            return NovoComm.Novo_GPIO_Get(mask,ref pData);
        }

        public int Novo_GPIO_Set(byte mask, byte data)
        {
            return NovoComm.Novo_GPIO_Set(mask,data);
        }

        public int Novo_IIC_Init(ref NOVO_IIC_CONFIG I2CConfig)
        {
            return NovoComm.Novo_IIC_Init(ref I2CConfig);
        }

        public int Novo_IIC_Rd_U16(byte devAddr, ushort regAddr, ref ushort pRegData)
        {
            bool  result = NovoComm.Novo_IIC_Rd_U16(devAddr, regAddr, ref pRegData) == 0 ? true : false;
            if (_logSwtich)
            {
                _logData.AddData(devAddr, "IIC Read", regAddr, pRegData.ToString("X2"), result, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff", CultureInfo.InvariantCulture));
            }
            
            return (!result).ToInt();
        }

        public int Novo_IIC_Rd_U16U32(byte devAddr, UInt16 regAddr, ref UInt32 pRegData)
        {
            //devAddr = (byte)((uint)devAddr * 2);
            bool result = NovoComm.Novo_IIC_Rd_U16U32(devAddr, (UInt16)(regAddr), ref pRegData) == 0 ? true : false;
            if (_logSwtich)
            {
                _logData.AddData((uint)devAddr >> 1, "IIC Read", regAddr, pRegData.ToString("X2"), result, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff", CultureInfo.InvariantCulture));
            }
            return (!result).ToInt();
        }

        public int Novo_IIC_Rd_U16U8(byte devAddr, ushort regAddr, ref byte pRegData)
        {
            bool result = NovoComm.Novo_IIC_Rd_U16U8(devAddr,regAddr,ref pRegData) == 0 ? true : false;
            if (_logSwtich)
            {
                _logData.AddData(devAddr, "IIC Read", regAddr, pRegData.ToString("X2"), result, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff", CultureInfo.InvariantCulture));
            }
            return (!result).ToInt();
        }

        public int Novo_IIC_Rd_U32(byte devAddr, uint regAddr, ref uint pRegData)
        {
            bool result = NovoComm.Novo_IIC_Rd_U32(devAddr, regAddr, ref pRegData) == 0 ? true : false;
            if (_logSwtich)
            {
                _logData.AddData(devAddr, "IIC Read", regAddr, pRegData.ToString("X2"), result, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff", CultureInfo.InvariantCulture));
            }
             return (!result).ToInt();
        }

        public int Novo_IIC_Rd_U8(byte devAddr, byte regAddr, ref byte pRegData)
        {
            bool result = NovoComm.Novo_IIC_Rd_U8(devAddr, regAddr, ref pRegData) == 0 ? true : false;
            if (_logSwtich)
            {
                _logData.AddData(devAddr, "IIC Read", regAddr, pRegData.ToString("X2"), result, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff", CultureInfo.InvariantCulture));
            }
            return (!result).ToInt();
        }

        public int Novo_IIC_RW_U8_Basic(byte devAddr, byte[] pWriteData, int writeLen, byte[] pReadData, int readLen)
        {
            byte[] data = new byte[2];
            Array.Copy(pWriteData, 1, data, 0, 2);
            Array.Reverse(data);
            UInt16 regAddr = BitConverter.ToUInt16(data, 0);
            string type = pReadData == null ? "IIC Write" : "IIC Read";
            bool result =  NovoComm.Novo_IIC_RW_U8_Basic(devAddr, pWriteData, writeLen, pReadData, readLen) == 0 ? true : false;
            if (_logSwtich)
            {
                StringBuilder resultBuilder = new StringBuilder();

                if (type == "IIC Read")
                {
                    // 遍历字节数组中的每个字节
                    foreach (byte b in pReadData)
                    {
                        // 将当前字节转换为其十六进制表示形式的字符串
                        string hexString = b.ToString("X2");

                        // 将当前字节的十六进制字符串追加到结果中
                        resultBuilder.Append(hexString);
                    }
                }
                else
                {
                    for (int i = 3; i < pWriteData.Length; ++i)
                    {
                        string hexString = pWriteData[i].ToString("X2");
                        resultBuilder.Append(hexString);
                    }
                }


                // 将 StringBuilder 中的内容转换为字符串
                string value = resultBuilder.ToString();
                _logData.AddData((uint)devAddr >> 1, type, regAddr, value, result, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff", CultureInfo.InvariantCulture));
            }
            return (!result).ToInt();
        }

        public int Novo_IIC_ScanDevAddr(byte[] pSlaveAddr)
        {
            return NovoComm.Novo_IIC_ScanDevAddr(pSlaveAddr);
        }

        public int Novo_IIC_SetIndex(int IICIndex)
        {
            return NovoComm.Novo_IIC_SetIndex(IICIndex);
        }

        public int Novo_IIC_Wr_U16(byte devAddr, ushort regAddr, ushort regData)
        {
            return NovoComm.Novo_IIC_Wr_U16(devAddr, regAddr, regData);
        }

        public int Novo_IIC_Wr_U16U32(byte devAddr, ushort regAddr, uint regData)
        {
            bool result = NovoComm.Novo_IIC_Wr_U16U32(devAddr, (UInt16)(regAddr),regData) == 0 ? true : false;
            if (_logSwtich)
            {
                _logData.AddData((uint)devAddr >> 1, "IIC Write", regAddr, regData.ToString("X2"), result, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff", CultureInfo.InvariantCulture));
            }
            return (!result).ToInt();
        }

        public int Novo_IIC_Wr_U16U8(byte devAddr, ushort regAddr, byte regData)
        {
            return NovoComm.Novo_IIC_Wr_U16U8(devAddr, regAddr, regData);
        }

        public int Novo_IIC_Wr_U32(byte devAddr, uint regAddr, uint regData)
        {
            return NovoComm.Novo_IIC_Wr_U32(devAddr,regAddr, regData);
        }

        public int Novo_IIC_Wr_U8(byte devAddr, byte regAddr, byte regData)
        {
            return NovoComm.Novo_IIC_Wr_U8(devAddr,regAddr,regData);
        }

        public int Novo_InitDev(ref NOVO_DEV_CONFIG pDevCfg)
        {
            return NovoComm.Novo_InitDev(ref  pDevCfg);
        }

        public int Novo_OpenDevice()
        {
            return NovoComm.Novo_OpenDevice();
        }

        public int Novo_SetDeviceId(int devID)
        {
            return NovoComm.Novo_SetDeviceId(devID);
        }
    }
}
